
Page({
  modalcnt: function () {
    wx.showModal({
      title: '提示',
      content: '这是一个模态弹窗',
      success: function (res) {
        if (res.confirm) {
          console.log('用户点击确定')
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  }  ,
  onLoad: function (options) {
    var that = this;
    //获取本地数据 https://mp.weixin.qq.com/debug/wxadoc/dev/api/data.html#wxremovestorageobject
    wx.getStorage({
      key: 'msgData',
      success: function (res) {
        // console.log(res.data);
        that.setData({ msgData: res.data });
      }
    });
  },
   
  data: {
    msgData: []
  },
  changeInput(ev) {
    // console.log(ev);
    this.setData({
      inputVal: ev.detail.value
    })
  },
  // 添加数据
  addMsg() {
    // console.log(this.data.inputVal);
    // 判断是否为空
    if (!this.data.inputVal == '') {
      var list = this.data.msgData;
      list.push({
        msg: this.data.inputVal
      });
      // 更新
      this.setData({
        msgData: list,
        inputVal: ''
      })
      wx.setStorage({
        key: "msgData",
        data: list,
      })
    } else {
      console.log('无数据')
    }

  },
  // 删除数据
  deleteMsg(ev) {
    // console.log(ev.target);
    var n = ev.target.dataset.index;
    var list = this.data.msgData;
    list.splice(n, 1);
    this.setData({
      msgData: list
    });
    wx.removeStorage({
      key: 'msgData',
      // success: function (res) {
      //   console.log(res.data)
      // }
    })
  }
  
})